Importing Data

Depending on the size of the data to be processed and the tools used, there are different data import methods. This section mainly introduces importing data using the object model and importing data using the pandas package.

Importing Data Using the Object Model

When the data volume is small, you can first import the data into an Excel worksheet, then use Excel, Excel functions, Excel VBA, or Python xlwings for data processing and analysis.

Section 1.3.1 introduced opening an existing Excel file using the Open method of the workbook object; this will not be repeated here.

Importing Data Using the pandas Package

Both Excel’s Power Query and Python’s pandas package were developed for data cleaning of large datasets, allowing easy processing of millions of rows. In contrast, Excel and Excel VBA can only handle hundreds of thousands of rows. For Power Query methods, readers may refer to relevant materials; this section focuses on how to import data using the pandas package.

Section 1.3.1 introduced opening an existing Excel file using the Open method of the workbook object; this will not be repeated here.

1. Reading/Writing Excel Files

The pandas package’s read_excel method can read Excel data. The read_excel method has many parameters; the commonly used ones are shown in Table 9-1. Using these parameters, you can import both regular and many irregular Excel datasets. The imported data is of DataFrame type.

Table 9-1 Commonly Used Parameters of the read_excel Method

Parameter Description
io Path and filename of the Excel file
sheet_name Name of the worksheet to read; can be a name or index. If not specified, reads the first worksheet.
header Specifies which row to use as the header (index row). For multi-index, use a list of row numbers.
index_col Specifies which column to use as the index column. For multi-index, use a list of column numbers or names.
usecols If only part of the columns are needed, specify them as a list.
dtype Dictionary to specify data types for particular columns, e.g., {"A": np.float64} sets column A to 64-bit float.
nrows Number of rows to read.
skiprows Number of rows to skip at the beginning.
skip_footer Number of rows to skip at the end.
names List to specify column index labels.
engine Engine for import, e.g., xlrd, openpyxl, etc.

Note: When using read_excel, errors such as missing xlrd may occur. It is recommended to install openpyxl and specify engine='openpyxl' when calling read_excel.

To install openpyxl:

Open the Command Prompt (Windows → Accessories → Command Prompt).

Enter:

code.bash
pip install openpyxl

Press Enter to install. A message like Finished processing dependencies for openpyxl indicates success.

The following Python script opens the Excel file ID_Numbers.xlsx in the current directory using pandas. The file contains two worksheets with staff information. The script imports data from the first worksheet. Script path: Samples\ch21\Python\ID_Numbers.py.

code.python
import pandas as pd   # Import pandas
import os             # Import os
root = os.getcwd()   # Get current path
# Read data from specified file
df = pd.read_excel(io=root + r'\ID_Numbers.xlsx', engine='openpyxl')
print(df)  # Output data

Running the script outputs the first worksheet data in the Python Shell:

code.vba
   ID Department Name                ID_Card Gender
0 1001   Finance  Chen Dong  5103211978100300**      F
1 1002   Finance  Tian Ju  4128231980052512**      M
2 1003 Production Wang Wei  4302251980031135**      M
3 1004 Production Wei Long  4302251985111635**      F
4 1005    Sales Liu Yang  4302251980081235**      F

By default, the first row is used as the header (column index), and row indices start from 0.

The sheet_name parameter can specify one or more worksheets; index_col can set a column as the row index. Below, we open two worksheets, set the “ID” column as the row index:

code.python
df2 = pd.read_excel(io='D:\ID_Numbers.xlsx', sheet_name=[0,1], index_col='ID', engine='openpyxl')
print(df2)

Output:

code.vba
{0:         Department Name                ID_Card Gender
ID
1001   Finance  Chen Dong  510321197810030016      F
1002   Finance  Tian Ju  412823198005251008      M
... ,
 1:         Department  Name                ID_Card Gender
ID
1006   Production  Lv Chuan  320325197001017024      F
... 
}

The result is a dictionary: keys are worksheet indices, values are DataFrames.

Use the DataFrame object’s to_excel method to write pandas data to an Excel file. For example, merge the first two worksheets and save to another file:

code.python
df3 = df2[0]  # Data from worksheet 1
df4 = df2[1]  # Data from worksheet 2
df5 = pd.concat([df3, df4])  # Vertical concatenation
print(df5)
df5.to_excel(root + r'\new_file.xlsx')

This saves merged data to new_file.xlsx.

2. Reading/Writing CSV Files

CSV is one of the most common data storage formats. Use pandas read_csv to read CSV files. Common parameters are in Table 9-2.

Table 9-2 Commonly Used Parameters of the read_csv Method

Parameter Description
filepath Path and filename of the CSV file
sep Delimiter; default is comma
header Row to use as header; for multi-index, use list of row numbers
index_col Column to use as index; for multi-index, use list of column numbers or names
usecols Columns to import, as a list
dtype Dictionary to specify column data types
prefix Prefix for columns if no header, e.g., Col produces Col0, Col1…
skiprows Rows to skip at the beginning
skipfooter Rows to skip at the end
nrows Number of rows to read
names List of column labels
encoding Encoding, default UTF-8, can also be GBK, etc.

Example script reading ID_Numbers.csv:

code.python
import pandas as pd
import os
root = os.getcwd()
df = pd.read_csv(root + r'\ID_Numbers.csv', encoding='gbk')
print(df)

To extract female staff and save:

code.python
df2 = df[df['Gender'] == 'F']
df2.to_csv(root + r'\new_file.csv', encoding='gbk')